import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoString {

    public static void test1(Jedis jedis) {
        System.out.println("mget 和 mset");
        jedis.flushAll();

        jedis.mset("key1", "111", "key2", "222", "key3", "333");

        List<String> mget = jedis.mget("key1", "key2", "key3");
        System.out.println(mget);
    }

    public static void test2(Jedis jedis) {
        System.out.println("getrange 和 setrange");

        jedis.set("key", "1234567890");
        String key = jedis.getrange("key", 0, 2);
        System.out.println("key: " + key);

        long result = jedis.setrange("key", 3, "abc");
        System.out.println("setrange: " + result);

        String key2 = jedis.get("key");
        System.out.println("key2: " + key2);
    }

    public static void test3(Jedis jedis) {
        System.out.println("append");
        jedis.flushAll();

        jedis.set("key", "123");
        long result = jedis.append("key", "456");
        System.out.println("result: " + result);

        String value = jedis.get("key");
        System.out.println("value: " + value);
    }

    public static void test4(Jedis jedis) {
        System.out.println("incr 和 decr");
        jedis.flushAll();

        jedis.set("key", "100");
        String key = jedis.get("key");
        System.out.println("key: " + key);

        long result = jedis.incr("key");
        System.out.println("result: " + result);

        String value = jedis.get("key");
        System.out.println("value: " + value);

        long result2 = jedis.decr("key");
        System.out.println("result2: " + result2);

        String value2 = jedis.get("key");
        System.out.println("value2: " + value2);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
